package com.ebelter.btcomlib.views;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import com.ebelter.btcomlib.R;
import com.ebelter.btcomlib.models.bluetooth.products.bracelet.bean.RateBean;
import com.ebelter.btcomlib.utils.DipUtil;
import com.ebelter.btcomlib.utils.log.LogUtils;

import java.util.List;

public class XLOneDayChartView extends View{
    public XLOneDayChartView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        init();
    }


    private Paint mPaint;
    private Paint mWarringPaint;
    private Paint mWarringPaint2;
    private int mWarringPaint2TextSize;

    private int color1;
    private int color2;
    private int color3 = getResources().getColor(R.color.form_line_color2);
    private int color4 = getResources().getColor(R.color.form_line_color);
    private void init() {
        mWarringPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
        mWarringPaint.setStyle(Paint.Style.STROKE);
        mWarringPaint.setStrokeWidth(DipUtil.dip2px(getContext(),1));
        mWarringPaint.setPathEffect(new DashPathEffect(new float[] {5,5,5,5}, 1));

        mWarringPaint2 = new Paint(Paint.ANTI_ALIAS_FLAG);//抗锯齿
        mWarringPaint2.setStyle(Paint.Style.FILL);
        mWarringPaint2TextSize=DipUtil.sp2px(getContext(),10);
        mWarringPaint2.setTextSize(mWarringPaint2TextSize);


        mPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
        mButtomPaintWith=DipUtil.dip2px(getContext(),1);
        mPaint.setStrokeWidth(mButtomPaintWith);
        mPaint.setTextSize(DipUtil.sp2px(getContext(),10));
    }

    private int mButtomPaintWith;

    public void setColor1(int color1) {
        this.color1 = color1;
    }
    public void setColor2(int color2) {
        this.color2 = color2;
    }

    private int mWidth;
    private int mHeight;

    private int textHeight;


    private int yStart;
    private int yEnd;

    private int warringValue;

    public void setWarringValue(int warringValue) {
        this.warringValue = warringValue;
    }

    private List<RateBean> rateOneDayInfos;

    public void setRateOneDayInfos(List<RateBean> rateOneDayInfos) {
        this.rateOneDayInfos = rateOneDayInfos;
        invalidate();
    }

    public void setYValue(int yStart, int yEnd) {
        this.yStart = yStart;
        this.yEnd = yEnd;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = getMeasuredWidth();
        mHeight = getMeasuredHeight();
        textHeight= (int) (mHeight*0.143);
    }


    boolean isDrawButtomTitle;
    String t1;
    String t2;
    String t3;
    public void setButtomTitle(boolean isDrawButtomTitle,String t1,String t2,String t3){
        this.isDrawButtomTitle=isDrawButtomTitle;
        this.t1=t1;
        this.t2=t2;
        this.t3=t3;
    }



    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);





        mPaint.setColor(color3);
        canvas.drawLine(0,mHeight-textHeight,mWidth,mHeight-textHeight,mPaint);//画水平横线



        if (isDrawButtomTitle){
            mPaint.setTextSize(DipUtil.sp2px(getContext(),10));
            for (int i=0;i<5;i++){

                float x= (float) (i*((mWidth-mPaddingRight-mPaddingLeft)*1.0/4))+mPaddingLeft;
                mPaint.setColor(color3);
                canvas.drawLine(x,mHeight-textHeight,x,mHeight-textHeight-15,mPaint);//画水平横线

                mPaint.setColor(color4);
                if (i==0){
                    canvas.drawText(t1,x,mHeight,mPaint);
                }else if (i==2){
                    Rect rect = getTextBounds(t2,mPaint);
                    x=x-rect.width()/2-mButtomPaintWith;
                    canvas.drawText(t2,x,mHeight,mPaint);
                }else if (i==4){
                    Rect rect = getTextBounds(t3,mPaint);
                    x=x-rect.width()-mButtomPaintWith;
                    canvas.drawText(t3,x,mHeight,mPaint);
                }


            }


        }


        drawVerticalAxis(canvas);


        if (rateOneDayInfos!=null&&rateOneDayInfos.size()!=0){
            mPaint.setColor(color2);
            for (int i=0;i<rateOneDayInfos.size();i++){


                long[] currentP = drawPosituon(canvas,i);
                long[] lastP = drawPosituon(canvas,i-1);

                if (lastP[0]!=-1&&lastP[1]!=-1&&lastP[2]!=-1){
                    float second = currentP[2]-lastP[2];

                    LogUtils.i("秒差值 = ",""+second);
                    if (second<=3900){ //3900是个权限值 65分钟内的要连线
                        canvas.drawLine(lastP[0],lastP[1],currentP[0],currentP[1],mPaint);
                    }
                }
            }
        }

        mWarringPaint.setColor(color1);
        mWarringPaint2.setColor(color1);

        float yWWeight = (float) ((warringValue-yStart)*1.0/(yEnd-yStart));//计算比重
        float yW = (mHeight-textHeight)*(1-yWWeight);


        canvas.drawLine(mPaddingLeft,yW, (float) (mWidth*0.94),yW,mWarringPaint);//画警告线

        canvas.drawText(warringValue+"", (float) (mWidth*0.94), (yW+mWarringPaint2TextSize/3),mWarringPaint2);



    }

    private void drawVerticalAxis(Canvas canvas) {
        mPaint.setTextSize(DipUtil.sp2px(getContext(),8));
        mPaint.setColor(color3);
        canvas.drawLine(mPaddingLeft,0, mPaddingLeft,mHeight-textHeight,mPaint);//画纵轴线


        if (rateOneDayInfos==null||rateOneDayInfos.size()==0){
            return;
        }

        float x = mPaddingLeft;//24*3600=86400

        int yValue=yStart+5;
        float yWeight = (float) ((yValue-yStart)*1.0/(yEnd-yStart));
        float y = (mHeight-textHeight)*(1-yWeight);
        canvas.drawLine(x,y,x-15,y,mPaint);



        int yValue2=yEnd-5;
        float yWeight2 = (float) ((yValue2-yStart)*1.0/(yEnd-yStart));
        float y2 = (mHeight-textHeight)*(1-yWeight2);
        canvas.drawLine(x,y2,x-15,y2,mPaint);

        mPaint.setColor(color4);
        canvas.drawText(yValue2+"",10,y2+textHeight/2,mPaint);
        canvas.drawText(yValue+"",10,y,mPaint);

    }






    private Rect getTextBounds(String text, Paint paint) {
        Rect rect = new Rect();
        paint.getTextBounds(text, 0, text.length(), rect);
        return rect;
    }

    private int mPaddingRight = DipUtil.dip2px(getContext(),8);
    private int mPaddingLeft = DipUtil.dip2px(getContext(),20);

    private long[] drawPosituon(Canvas canvas,int i) {

        if (i<0||i>rateOneDayInfos.size()-1){
            return new long[]{-1,-1,-1};
        }


        RateBean rateBean = rateOneDayInfos.get(i);

        float x = (float) ((mWidth-mPaddingLeft-mPaddingRight)*rateBean.timeLong*1.0/86400)+mPaddingLeft;//24*3600=86400

        int yValue=rateBean.rate;
        float yWeight = (float) ((yValue-yStart)*1.0/(yEnd-yStart));
        float y = (mHeight-textHeight)*(1-yWeight);

        canvas.drawPoint(x,y,mPaint);

        return new long[]{(long) x, (long) y,rateBean.timeLong};
    }
}
